草庐IT

特征的 PHP instanceof

全部标签

php - 如何捕获特征未找到错误 PHP 7

从PHP7开始,可以通过捕获Error类或Throwable接口(interface)来捕获FatalErrors,但由于某些原因我不能当“fatalerror:未找到特征”被触发时,我们无法做到这一点。try{classCars{usePrice;}}catch(Error$e){echo$e->getMessage();//Output:Fatalerror:Trait'Price'notfoundin[..]online[..]}没有发现错误!!所以我想出了一个解决方案try{if(trait_exists('Price')){classCars{usePrice;}}else{

PHP:是否可以使用特征静态方法中的特征获取类的名称?

可以从属于该特征的静态方法中确定使用特征的类的名称吗?例如:traitSomeAbility{publicstaticfunctiontheClass(){return;}}classSomeThing{useSomeAbility;...}获取类名:$class_name=SomeThing::theClass();我的直觉是,可能不会。我没能找到任何其他建议。 最佳答案 使用latestaticbindingwithstatic:traitSomeAbility{publicstaticfunctiontheClass(){re

php - 未找到 Laravel 自定义特征

我是traits的新手,但我想试试看。但是,它似乎没有加载。我在Laravel应用程序目录下的一个文件夹中创建了一个特征:app\Helpers名为CheckPermsAgainstObjectTrait.php这是特征代码:我尝试在Controller中使用它:该目录中的类加载正常。PHPStorm认为这个特征很好。我已经清除了已编译的aritsan并转储了自动加载。我猜Laravel不喜欢命名空间?我希望我不需要在composer中进行任何手动加载——但我找不到任何文档来提示我搞砸了什么。错误:FatalErrorExceptioninPolicyController.phplin

php - Codeception 特征 'Codeception\Specifiy' 未找到

我有一个PHP应用程序,我们正在使用Codeception添加测试。我们尝试将Specify(和Verify)添加到我们的套件中,但未被识别。您如何正确设置Specify以与Codeception一起使用?在我的composer.json中,我有以下内容:{"require-dev":{"codeception/codeception":"2.0.11","codeception/specify":"*","codeception/verify":"*"}}我在添加指定和验证后运行了composerupdate并得到了这个输出:Loadingcomposerrepositorieswi

php - 我可以覆盖消费者类中的 PHP 特征属性以使用 Doctrine2 注释吗?

我正在使用特征在Symfony应用程序中实现一些可标记的行为,使用Doctrine2实现持久性,并使用注释来配置它。我的主要烦恼是,在特征中,我的IDE不知道$this->标签的类型,并抛出一堆警告。我对在此处记录我的代码非常强制症,这样其他开发人员就很容易上手。traitTaggableMethods{/**@var\Doctrine\Common\Collections\Collection*/protected$tags;//tags->add($tag);}publicfunctionremoveTag(Tag$tag){$this->tags->removeElement($

php - Doctrine2 从特征中复制属性和方法

我有一个实体,我想在其中使用特征“TimestampableEntity”来映射一些属性:namespaceWbudowie\PortalBundle\Entity;useDoctrine\ORM\MappingasORM;useGedmo\Mapping\AnnotationasGedmo;useWbudowie\PortalBundle\Traits\TimestampableEntity;/***Category**@Gedmo\Tree(type="materializedPath")*@Gedmo\SoftDeleteable(fieldName="deletedAt",ti

php - 以编程方式识别覆盖特征方法的方法

我正在尝试在覆盖特征方法的类中生成一组方法。这是一个简化的示例,显示了我的应用程序中典型类和特征的方法结构:classdemo{publicfunctionMethodA(){return'methodAfromclass';}publicfunctionMethodC(){return'methodCfromclass';}publicfunctionMethodE(){return'methodEfromclass';}usetGeneric;}traittGeneric{publicfunctionMethodA(){return'methodAfromtrait';}publi

php - 在类上使用特征,为什么?

类是怎么做的?ClassMain{$this->a=newA();$this->b=newB();$this->c=newC();$this->b->doTranslate($this->a->saySomething());}这就是traits的工作方式,不是吗?ClassMain{useA;useB;useC;$this->doTranslate($this->saySomething());}我对traits了解不多,但通过查看新的PHP5.4trait示例,它们似乎只在一个案例中有所帮助。一个类只能扩展一次以一起使用$this,但我们可以使用多个特征。问题1:这是使用trait

php - 多个特征同时使用相同的基本特征

好吧,假设存在以下问题:traitBase{publicfunctiondoSomething(){//Dofancystuffneededinothertraits}}traitA{useBase;publicfunctionfoo(){//Dosomething}}traitB{useBase;publicfunctionbar(){//Dosomethingelse}}现在我想实现一个同时使用特征A和B的类:classMyClass{useA,B;}PHP告诉我它不能重新定义函数doSomething()。PHP无法检测到A和B共享同一个特征并且不将其复制到MyClass中的原因

PHP 实现一个特征,覆盖然后调用它的父特征

我正在使用Laravel5.3..Model.php具有以下功能:publicfunctionforceDelete(){return$this->delete();}但是,我正在使用具有以下功能的softDeletes特性来实现它:publicfunctionforceDelete(){$this->forceDeleting=true;$deleted=$this->delete();$this->forceDeleting=false;return$deleted;}但是,我用这个覆盖了这个函数:publicfunctionforceDelete(){echo"deleting"